package cn.anecansaitin.whimsy.common.effect.ow.doomfist;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.client.registers.ModSounds;
import cn.anecansaitin.whimsy.common.capabilities.battle.DoomfistBattle;
import cn.anecansaitin.whimsy.common.capabilities.battle.IBattleMode;
import cn.anecansaitin.whimsy.common.register.ModEffects;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.entity.living.LivingHurtEvent;
import net.neoforged.neoforge.event.entity.living.MobEffectEvent;
import org.joml.Vector3f;

//@Mod.EventBusSubscriber(modid = Whimsy.MODID)
public class PowerBlock extends MobEffect {
    public PowerBlock() {
        super(MobEffectCategory.BENEFICIAL, 0);
    }

    @Override
    public void onEffectStarted(LivingEntity livingEntity, int amplifier) {
        if (!livingEntity.level().isClientSide) {
            livingEntity.level().playSound(null, livingEntity.getX(), livingEntity.getY(), livingEntity.getZ(), ModSounds.POWER_BLOCK.get(), SoundSource.PLAYERS);
        }
    }

    @SubscribeEvent
    public static void onHurt(LivingHurtEvent event) {
        LivingEntity target = event.getEntity();

//        if (!target.hasEffect(ModEffects.POWER_BLOCK.get())) {
//            return;
//        }

        DamageSource source = event.getSource();
        Vec3 sourcePos = null;

        if (source.getDirectEntity() != null) {
            sourcePos = source.getDirectEntity().position();
        } else if (source.getEntity() != null) {
            sourcePos = source.getEntity().position();
        } else if (source.getSourcePosition() != null) {
            sourcePos = source.getSourcePosition();
        }

        //无来源的伤害与来源与正面地伤害可以减免
        if (sourcePos == null || checkDamageDirection(sourcePos, target)) {
            float amount = event.getAmount();
            event.setAmount(amount * 0.2f);
            IBattleMode battleMode = target.getCapability(IBattleMode.BATTLE_MODE);

            if (battleMode == null || !(battleMode.getController() instanceof DoomfistBattle doomfistBattle)) {
                return;
            }

            //吸收能量
//            doomfistBattle.blockDamage(target, amount - event.getAmount());
        }
    }

    private static boolean checkDamageDirection(Vec3 pos, LivingEntity entity) {
        Vector3f entityPos = entity.position().toVector3f();
        Vector3f enemyPos = pos.toVector3f();
        Vector3f enemyTOEntity = entityPos.sub(enemyPos);
        Vector3f view = entity.getViewVector(1).toVector3f();
        float dot = enemyTOEntity.dot(view);
        float cos = dot / (enemyTOEntity.length() * view.length());
        return Math.toDegrees(Math.acos(cos)) < 180;
    }

    @SubscribeEvent
    public static void onEffectRemove(MobEffectEvent.Remove event) {
//        if (event.getEffect() == ModEffects.POWER_BLOCK.get()) {
//            onEffectFinished(event.getEntity());
//        }
    }

    @SubscribeEvent
    public static void onEffectExpired(MobEffectEvent.Expired event) {
//        if (event.getEffectInstance().getEffect() == ModEffects.POWER_BLOCK.get()) {
//            onEffectFinished(event.getEntity());
//        }
    }

    private static void onEffectFinished(LivingEntity entity) {
        IBattleMode battleMode = entity.getCapability(IBattleMode.BATTLE_MODE);

        if (battleMode == null || !(battleMode.getController() instanceof DoomfistBattle doomfistBattle)) {
            return;
        }

//        doomfistBattle.blockCompleted();
    }
}
